package com.dataart.training.crimequalification.beans;

import com.dataart.training.crimequalification.entity.TaskSolutionResultEntity;
import com.dataart.training.crimequalification.entity.UserEntity;
import com.dataart.training.crimequalification.service.AuthenticationService;
import com.dataart.training.crimequalification.service.QualificationTaskService;
import com.dataart.training.crimequalification.service.UserService;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import java.util.Date;
import java.util.List;

@Component
@ManagedBean
@Scope("request")
public class PreviousResultsBean {

    @Autowired
    private QualificationTaskService taskService;

    @Autowired
    private UserService userService;

    @Autowired
    private AuthenticationService authenticationService;

    private List<TaskSolutionResultEntity> tasks;

    @PostConstruct
    private void init() {
        UserEntity user = authenticationService.getCurrentLoggedInUser(userService);
        tasks = taskService.getAllUserSolutions(user);
    }

    public boolean solutionPassed(TaskSolutionResultEntity t) {
        return t.getPercentage() >= 50;
    }

    public String formatDate(Date date) {

        DateTime dt = new DateTime(date);
        final DateTimeFormatter parseFormatter = DateTimeFormat.forPattern("dd/MM/YYYY 'в' HH:mm:ss");
        return dt.toString(parseFormatter);

    }

    public List<TaskSolutionResultEntity> getTasks() {
        return tasks;
    }

    public void setTasks(List<TaskSolutionResultEntity> tasks) {
        this.tasks = tasks;
    }
}
